package com.bdsy.business.controller;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.bdsy.api.annotation.IgnoreAuth;
import com.bdsy.business.model.ShopImage;
import com.bdsy.business.service.ShopImageService;
import com.bdsy.business.service.ShopSellInfoService;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.ImageComparetor;
import com.bdsy.common.utils.Result;
import com.bdsy.system.annotation.SysLog;
import com.bdsy.system.model.User;



@Controller
@RequestMapping(value = { "${web.adminPath}", "${web.appPath}" })
public class ShopImageController {

	@Autowired
	ShopImageService shopImageService;
	
	@Autowired
	ShopSellInfoService shopSellInfoService;
	
	/**
	 * 采集商品信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "cShopInfo")
	public Object cShopInfo(MultipartFile file,ShopImage shopImage,String icdn) {

		List<Map> shopInfoList= shopSellInfoService.selectShopSellInfoByIcdn(icdn);
		Integer shopId = null;
		for (int i = 0; i < shopInfoList.size(); i++) {
			shopId = (Integer) shopInfoList.get(0).get("shop_id");
		}
		shopImage.setShopId(shopId);
		boolean result = shopImageService.updateImage(shopImage, file);
		if (result) {
			return Result.ok("updateSuccess");
		} else {
			return Result.error("updateFail");
		}
	}
	
	
	@RequestMapping(value = "imageContrast")
	public String imageContrast(HttpServletRequest request) {
		return "admin/business/imageContrast";
	}
	
	/**
	 * 图片对比
	 */
	@Json
	@RequestMapping(value = "imageContrasts")
	public Object imageContrasts(@Param(value="file1")MultipartFile file1,@Param(value="file2")MultipartFile file2) {
		
		CommonsMultipartFile cf= (CommonsMultipartFile)file1;  
		DiskFileItem fi = (DiskFileItem)cf.getFileItem();  
		File f = fi.getStoreLocation();
		CommonsMultipartFile cf2= (CommonsMultipartFile)file2;  
		DiskFileItem fi2 = (DiskFileItem)cf2.getFileItem();  
		File f2 = fi2.getStoreLocation();
		
		float xs = ImageComparetor.compareImage(f, f2);
		System.out.println("-----------------"+xs);
		Map<String,Object> map = new LinkedHashMap<>();
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", xs);
		return map;
	}
}
